package com.zzl.study.controller;

import com.zzl.study.entity.User;
import com.zzl.study.manager.UserOnlineManager;
//import io.micrometer.core.instrument.Gauge;
//import io.micrometer.core.instrument.MeterRegistry;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

/**
 * @author: zhangzl
 * @date: 2024/3/6 15:37
 * @version: 1.0
 * @description: 用户Controller
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserOnlineManager userOnlineManager;

//    @Autowired
//    private MeterRegistry meterRegistry;

    @PostMapping("/login")
    public void login(@RequestBody User user) {
        this.userOnlineManager.online(user);
        log.info("用户 {} 上线!", user);
    }

    @PostMapping("/logout/{id}")
    public void logout(@PathVariable("id") Long id) {
        this.userOnlineManager.offline(id);
        log.info("用户 {} 下线!", id);
    }

//    @Scheduled(cron = "0/5 * * * * ? ")
//    public void meter() {
//        Gauge.builder("online_user_count", userOnlineManager, UserOnlineManager::getOnlineUserCount)
//                .baseUnit("人")
//                .description("在线人数")
//                .tag("system", "user")
//                .strongReference(false)
//                .register(this.meterRegistry);
//    }

}
